package com.butterfly.hotkey.worker.util;

import com.butterfly.hotkey.core.constant.Constants;
import io.netty.channel.ChannelHandlerContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.net.InetSocketAddress;

/**
 * Netty Util
 *
 * @author butterfly
 */
public class NettyIpUtil {

    private static final Logger log = LoggerFactory.getLogger(Constants.WORKER);

    /**
     * 从netty连接中读取ip地址
     */
    public static String clientIp(ChannelHandlerContext ctx) {
        try {
            InetSocketAddress inetSocketAddress = (InetSocketAddress) ctx.channel()
                    .remoteAddress();
            return inetSocketAddress.getAddress().getHostAddress();
        } catch (Exception e) {
            log.error("get netty client ip fail", e);
        }
        return "UNKNOWN";
    }
}